package wiki;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Root;

/**
 * Servlet implementation class UpdateIssue
 */
public class UpdateIssue extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateIssue() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		Root root = new Root();
		ResultSet rs = null;
		String sql = "";
		PreparedStatement pst=null;
		try {
			String action = "";
			action = request.getParameter("action");
			//System.out.println("Action: " + action);

			if ((action != null) || (!action.equals(""))) {

				if (action.equalsIgnoreCase("addcomment")) {
					// add comment

					String issueid = "";
					String comments = "";
					String username = "";
					
					if ((issueid != null) || (!issueid.equals(""))) {

						issueid = request.getParameter("issueid");
						comments = request.getParameter("comments");
						username = request.getParameter("username");
						
						root.initConnection();
						
						comments=comments.replaceAll("\\n", "<br/>");
						
						
						sql = "insert into issuedetails "
								+ "(issueid, comment, messagetype, username ,date)"
								+ "values (?, ?, ?, ?, CURRENT_DATE());";

						pst = root.con.prepareStatement(sql);
						pst.setString(1, issueid);
						pst.setString(2, comments);
						pst.setString(3, "1");
						pst.setString(4, username);

						//System.out.println("SQL: " + pst);
						pst.executeUpdate();
					}

				}else if (action.equalsIgnoreCase("deletecomment")) {
					
					
					// delete comment

					String id = "";
					id = request.getParameter("commentid");
					//System.out.println("Delete Comment: " + id);
					

					if ((id != null) || (!id.equals(""))) {

						root.initConnection();
						sql = "update issuedetails set deleteflag = 1 where id= ?";

						pst = root.con.prepareStatement(sql);
						pst.setString(1, id);
						//System.out.println("SQL: " + pst);
						pst.executeUpdate();
					}

				}else if (action.equalsIgnoreCase("editcomment")) {
					// delete comment

					String id = "";
					String comment = "";
					String username ="";
					
					id = request.getParameter("commentid");
					comment = request.getParameter("comments");
					username = request.getParameter("username");
					
					//System.out.println("Edit Comment: " + id);
					
					if ((id != null) || (!id.equals(""))) {

						root.initConnection();
						sql = "update issuedetails set comment = ?,  username = ?, date=CURRENT_DATE() where id= ? limit 1";

						pst = root.con.prepareStatement(sql);
						pst.setString(1, comment);	
						pst.setString(2, username);	
						pst.setString(3, id);
						
						//System.out.println("SQL: " + pst);
						pst.executeUpdate();
					}

				}else if (action.equalsIgnoreCase("deleteissue")) {
					
					
					// delete comment

					String id = "";
					id = request.getParameter("issueid");
					//System.out.println("Delete issue: " + id);
					
					int nid=0;
					
					nid=Integer.parseInt(id);
					
					if ((id != null) || (!id.equals(""))) {

						root.initConnection();
						sql = "update issue set deleteflag = 1 where id= ?";

						pst = root.con.prepareStatement(sql);
						pst.setInt(1, nid);
						//System.out.println("SQL: " + pst);
						pst.executeUpdate();
					}

				}

			} 

		} catch (Exception e) {
			System.out.println("Exception :" + e.toString());
		}finally{
			try {
				pst.close();
				rs.close();
				root.closeConnection();
			} catch (Exception e) {
				
			}
		}

	}

}
